package cn.virens.server.aio.packet;

import cn.virens.common.utils.number.ByteUtil;
import cn.virens.server.common.ReadPacket;
import cn.virens.server.common.WritePacket;

public class AioWritePacket extends AioPacket implements WritePacket {

	@Override
	public void setSeq(int seq) {
		this.seq = seq % 255;
	}

	@Override
	public void setLength(int len) {
		this.length = len;
	}

	@Override
	public void setCommand(int cmd) {
		this.command = cmd;
	}

	@Override
	public void setData(byte[] data) {
		this.data = data;
	}

	@Override
	public void setData(String hex) {
		this.data = ByteUtil.toByte(hex);
	}

	@Override
	public byte[] seqByte() {
		return ByteUtil.toByte(seq, 1);
	}

	@Override
	public byte[] lenByte() {
		return ByteUtil.toByte(length, 2);
	}

	@Override
	public byte[] cmdByte() {
		return ByteUtil.toByte(command, 1);
	}

	@Override
	public byte[] dataByte() {
		return data;
	}

	public static AioWritePacket of(ReadPacket packet) {
		AioWritePacket writePacket = new AioWritePacket();
		writePacket.setCommand(packet.getCommand());
		writePacket.setSeq(packet.getSeq());

		return writePacket;
	}

	@Override
	public String toString() {
		return "WritePacket [cmd=" + command + ", data=" + ByteUtil.toHex(data) + "]";
	}
}
